package com.base.mail;

import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.base.commonlib.base.CBaseBarMvpActivity;
import com.base.mail.databinding.ActivityAjconstractBinding;
import com.base.mail.presenter.AjConstractPresenter;
import com.base.mail.view.AjConstractView;

/**
 * 安监通讯录
 * Created by weikailiang on 2019/11/20.
 */
@Route(path = "/ajconstract/constract")
public class AjConstractActivity extends CBaseBarMvpActivity<AjConstractPresenter> implements AjConstractView{
    private ActivityAjconstractBinding mBinding;
    private FragmentManager mManager;
    private PresonFragment mPresonFragment;
    private DepartFragment mDepartFragment;
    @Override
    protected int getLayoutResource() {
        return R.layout.activity_ajconstract;
    }

    @Override
    protected void init() {
        super.init();
        mBinding = (ActivityAjconstractBinding) mRootBinding;
        mPresenter = new AjConstractPresenter(this,this);
        setBackNavigation();
        setBarTitle("通讯录");
    }

    @Override
    protected void start() {
        mManager = getSupportFragmentManager();

        mBinding.tab.setSelectedTabIndicatorHeight(0);
        final TabLayout.Tab tab1 = mBinding.tab.newTab();
        tab1.setText("人员");
        mBinding.tab.addTab(tab1, true);
        TabLayout.Tab tab2 = mBinding.tab.newTab();
        tab2.setText("部门");
        mBinding.tab.addTab(tab2, false);
        mBinding.tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                FragmentTransaction transaction = mManager.beginTransaction();
                if (tab.getPosition() == 0) {
                    if (mPresonFragment == null){
                        mPresonFragment = new PresonFragment();
                        transaction.add(R.id.contain,mPresonFragment);
                    }else {
                        transaction.show(mPresonFragment);
                    }
                    if (mDepartFragment != null){
                        transaction.hide(mDepartFragment);
                    }


                } else {
                    if (mDepartFragment == null){
                        mDepartFragment = new DepartFragment();
                        transaction.add(R.id.contain,mDepartFragment);
                    }else {
                        transaction.show(mDepartFragment);
                    }
                    if (mPresonFragment != null){
                        transaction.hide(mPresonFragment);
                    }

                }
                transaction.commit();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        FragmentTransaction transaction = mManager.beginTransaction();
        mPresonFragment = new PresonFragment();
        transaction.add(R.id.contain,mPresonFragment);
        transaction.commit();
    }
}
